package com.taotaojs.util;

/**
 * 字符串工具类
 *
 * @author: TaoTaojs
 * @Date: 2020/4/22 1:37 PM
 * @Description:
 * <h1>字符串工具类</h1>
 * <h2>简介</h2>
 * 字符串工具类
 * <h3>主要功能：</h3>
 * <ol>
 *     <li>将集合转换为用分隔符分隔的字符串</li>
 *     <li>判断字符串对象是否为空</li>
 * </ol>
 * @关联类:
 * @参考链接：
 * @History:
 * <author>        <time>                      <version>          <desc>
 * TaoTaojs        2020/4/22 1:37 PM           V1.0               增加空，非空，非空白三种判断方法
 * TaoTaojs        2020年07月06日10:41:15       V1.0               将集合转换为用分隔符分隔的字符串
 */
public class StringUtil {

    public static final String EMPTY = ",";

    private StringUtil(){}

    public static boolean isNotEmpty(String str) {
        return str != null && str.trim().length()>0;
    }

    public static boolean isEmpty(String str) {
        return str == null || str.length()==0;
    }

    public static boolean isBlank(String str){
        int strLen;
        if (str == null || (strLen = str.length()) == 0) {
            return true;
        }
        for (int i = 0; i < strLen; i++) {
            if ((!Character.isWhitespace(str.charAt(i)))) {
                return false;
            }
        }
        return true;
    }

    public static boolean isNotBlank(String str){
        return !isBlank(str);
    }

    /**
     * 将集合转换为用分隔符分隔的字符串
     * @param array
     * @param separator
     * @param startIndex
     * @param endIndex
     * @return
     */
    public static String join(final Object[] array, String separator, final int startIndex, final int endIndex) {
        if (array == null) {
            return null;
        }
        //如果分隔字符为空，就设置为默认分隔字符
        if (separator == null) {
            separator = EMPTY;
        }

        final int noOfItems = endIndex - startIndex;
        if (noOfItems <= 0) {
            return EMPTY;
        }

        //开始拼接逻辑
        final StringBuilder buf = new StringBuilder(noOfItems);
        for (int i = startIndex; i < endIndex; i++) {
            if (i > startIndex) {
                buf.append(separator);
            }
            if (array[i] != null) {
                buf.append(array[i]);
            }
        }
        return buf.toString();
    }
    public static String join(final Object[] array){
        return join(array, ",");
    }
    public static String join(final Object[] array, String separator){
        if (array == null) {
            return null;
        }
        return join(array, separator, 0, array.length);
    }

    /**
     * 判断两个对象是否相同
     * @param str1
     * @param str2
     * @return
     */
    public static boolean equals(String str1, String str2) {
        if(str1 == null || str2 == null){
            return false;
        }
        return str1.equals(str2);
    }
}
